﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net;
using System.Windows;

public class YoutubeVideo
{
    public string VideoID { get; set; }

    public string VideoTitle { get; set; }

    public string Extention { get; set; }

    public string DownloadUrl { get; set; }

    public string VideoUrl { get; set; }

    public long VideoSize { get; set; }

    public Size Dimension { get; set; }

    public long Length { get; set; }

    public void SetAll()
    {
        SetSize();
        SetQuality();

    }

    private void SetSize(long size)
    {
        this.VideoSize = size;
    }

    private void SetSize()
    {
        try
        {
            HttpWebRequest fileInfoRequest = (HttpWebRequest)HttpWebRequest.Create(this.DownloadUrl);
            HttpWebResponse fileInfoResponse = (HttpWebResponse)fileInfoRequest.GetResponse();
            long bytesLength = fileInfoResponse.ContentLength;
            fileInfoRequest.Abort();
            if (bytesLength != -1)
            {
                this.SetSize(bytesLength);
            }
        }
        catch (Exception generatedExceptionName)
        {
        }
    }

    private void SetQuality(string Extention, Size Dimension)
    {
        this.Extention = Extention;
        this.Dimension = Dimension;
    }

    public void SetQuality()
    {
        this.SetQuality("Unknown", new Size(0, 0));
        if (this.DownloadUrl.Contains("itag=5"))
        {
            this.SetQuality("flv", new Size(320, 240));
        }
        else if (this.DownloadUrl.Contains("itag=6"))
        {
            this.SetQuality("flv", new Size(480, 360));
        }
        else if (this.DownloadUrl.Contains("itag=17"))
        {
            this.SetQuality("3gp", new Size(176, 144));
        }
        else if (this.DownloadUrl.Contains("itag=18"))
        {
            this.SetQuality("mp4", new Size(480, 360));
        }
        else if (this.DownloadUrl.Contains("itag=22"))
        {
            this.SetQuality("mp4", new Size(1280, 720));
        }
        else if (this.DownloadUrl.Contains("itag=34"))
        {
            this.SetQuality("flv", new Size(400, 226));
        }
        else if (this.DownloadUrl.Contains("itag=35"))
        {
            this.SetQuality("flv", new Size(640, 380));
        }
        else if (this.DownloadUrl.Contains("itag=36"))
        {
            this.SetQuality("3gp", new Size(320, 240));
        }
        else if (this.DownloadUrl.Contains("itag=37"))
        {
            this.SetQuality("mp4", new Size(1920, 1080));
        }
        else if (this.DownloadUrl.Contains("itag=38"))
        {
            this.SetQuality("mp4", new Size(2048, 1080));
        }
        else if (this.DownloadUrl.Contains("itag=43"))
        {
            this.SetQuality("webm", new Size(480, 360));
        }
        else if (this.DownloadUrl.Contains("itag=44"))
        {
            this.SetQuality("webm", new Size(640, 480));
        }
        else if (this.DownloadUrl.Contains("itag=45"))
        {
            this.SetQuality("webm", new Size(1280, 720));
        }
        else if (this.DownloadUrl.Contains("itag=46"))
        {
            this.SetQuality("webm", new Size(1920, 1080));
        }
        else if (this.DownloadUrl.Contains("itag=82"))
        {
            this.SetQuality("mp4", new Size(640, 360));
        }
        else if (this.DownloadUrl.Contains("itag=83"))
        {
            this.SetQuality("mp4", new Size(854, 480));
        }
        else if (this.DownloadUrl.Contains("itag=84"))
        {
            this.SetQuality("mp4", new Size(1280, 720));
        }
        else if (this.DownloadUrl.Contains("itag=85"))
        {
            this.SetQuality("mp4", new Size(1920, 1080));
        }
        else if (this.DownloadUrl.Contains("itag=133"))
        {
            this.SetQuality("mp4", new Size(0, 0));
        }
        else if (this.DownloadUrl.Contains("itag=140"))
        {
            this.SetQuality("m4a", new Size(0, 0));
        }
        else if (this.DownloadUrl.Contains("itag=139"))
        {
            this.SetQuality("m4a", new Size(0, 0));
        }
        else if (this.DownloadUrl.Contains("itag=160"))
        {
            this.SetQuality("mp4", new Size(0, 0));
        }
        else if (this.DownloadUrl.Contains("itag=136"))
        {
            this.SetQuality("mp4", new Size(0, 0));
        }
        else if (this.DownloadUrl.Contains("itag=134"))
        {
            this.SetQuality("mp4", new Size(0, 0));
        }
        else if (this.DownloadUrl.Contains("itag=141"))
        {
            this.SetQuality("m4a", new Size(0, 0));
        }
        else if (this.DownloadUrl.Contains("itag=135"))
        {
            this.SetQuality("mp4", new Size(0, 0));
        }
    }

    public override string ToString()
    {
        return String.Format("{0} ({1}-{2}) - {3}", 
            Extention.ToUpper(), Dimension.Width, Dimension.Height, formatSizeBinary(VideoSize));

    }
    private string formatSizeBinary(Int64 size, Int32 decimals = 2)
    {
        string[] sizes = {
		"Bytes",
		"KB",
		"MB",
		"GB",
		"TB",
		"PB",
		"EB",
		"ZB",
		"YB"
	};
        double formattedSize = size;
        Int32 sizeIndex = 0;
        while (formattedSize >= 1024 & sizeIndex < sizes.Length)
        {
            formattedSize /= 1024;
            sizeIndex += 1;
        }
        return string.Format("{0} {1}", Math.Round(formattedSize, decimals).ToString(), sizes[sizeIndex]);
    }
}